<?php


namespace App\Models\Admin;


use App\Log\AdminGroupClientUserLog;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class AdminGroupClientUser extends  Authenticatable implements JWTSubject
{
    const OPERATE_PID = 1;
    const MODEL = 'admin_group_client_user';

    protected $table = 'admin_group_client_user';
    protected $guarded = [];
    protected $dateFormat = 'U';
    protected $hidden = [
        'password',
    ];
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return ['role' => 'group'];
    }

    public function role()
    {
        return $this->belongsTo(AdminGroupClientRole::class,'role_id','id');
    }

    public static function getList($where)
    {
        $list=self::getwhere($where)
            ->with([
                'role:id,name'
            ])
            ->select()
            ->get();
        return $list;
    }

    public static function getWhere($where)
    {
        $select=self::where([]);
        if(!empty($where['group_client_id']))
            $select=$select->where('group_client_id',$where['group_client_id']);
        if(!empty($where['role_id']))
        {
            $select=$select->where('role_id',$where['role_id']);
        }
        return $select;
    }

    public static function addLog($pre,$next,$type,$method='')
    {
        $info = AdminOperateModule::where('pid',self::OPERATE_PID)->where('type',$type)->where('model',self::MODEL)->when($method,function ($query)use ($method){
            $query->where('method',$method);
        })->first();
        if (!empty($info)){
            $adminLog = new AdminGroupClientUserLog($pre,$next,$info);
            $adminLog->addLog();
        }
    }
}
